package unq.uis.robots.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.uqbar.commons.model.UserException;

import unq.uis.robots.applicationsModels.ComprarRobotApplicationModel;

@SuppressWarnings("serial")
public class RetornarDeCompraServlet extends HttpServlet{
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		ComprarRobotApplicationModel comprarRobotApplicationModel = (ComprarRobotApplicationModel)request.getSession().getAttribute("comprar");		
		int oferta = Integer.parseInt(request.getParameter("oferta"));
		comprarRobotApplicationModel.setOferta(oferta);
		
		try{
		comprarRobotApplicationModel.ofertar();
		request.getRequestDispatcher("jugador.jsp").forward(request, response);		
		}catch (UserException e) {
			 this.mostrarError(request, response, e.getMessage());
		}
	}	
	
	private void mostrarError(HttpServletRequest request,
			HttpServletResponse response, String message) throws ServletException, IOException {
		request.getSession().setAttribute("error", message);
		request.getRequestDispatcher("ventanaDeError.jsp").forward(request, response);
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		request.getRequestDispatcher("jugador.jsp").forward(request, response);
	}

}